//
//  AppointmentViewController.m
//  WheatPlan
//
//  Created by 王晨辉 on 15/5/27.
//  Copyright (c) 2015年 IOSTeam. All rights reserved.
//

#import "AppointmentViewController.h"
#import "IndividualViewController.h"
#import "UIImageView+WebCache.h"
@interface AppointmentViewController (){
    
    NSArray *_provinceArray;
    NSString * strr;
    
}

@end

@implementation AppointmentViewController


-(void)viewWillAppear:(BOOL)animated{
    
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString *babyID = [user1 objectForKey:@"babyID"];
    ////NSLog(@"%@dudududududududududu",babyID);

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置导航栏头文字
    self.navigationItem.title = @"选择早教指导师";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#ef5858" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];    //左边按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;

    
    
    int a =  self.navigationController.navigationBar.frame.size.height+20;

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.rowHeight = (SCREEN_HEIGHT-a)/4;
    [self.view addSubview:_tableView];
    
    [MBProgressHUD showHUDAddedTo:_tableView animated:YES];
    
    
    teacherNames  = [NSMutableArray array];
    otherStrs  = [NSMutableArray array];
    strs  = [NSMutableArray array];
    identifya  = [NSMutableArray array];
    priceSS  = [NSMutableArray array];
    statusS  = [NSMutableArray array];

    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];
    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString *babyID = [user1 objectForKey:@"babyID"];
    ////NSLog(@"%@dudududududududududu",babyID);
    
    NSDictionary *param3 = @{/*@"contentId": @"3",*/@"userId":ID,@"babyId":[BabyManager shareInstance].babyId};
    
    [UserDAL getTeachersParams:param3 withBlock:^(NSArray *data, NSError *error) {
        
        
        //NSLog(@"what%@11223344",data);
        
        
        if (data.count!=0) {
            for (NSDictionary *object in data) {
                
                ////NSLog(@"what%@11223344",object);
                
                NSString * teacherName =   [object objectForKey:@"teacherName"];
                NSString * otherStr = [object objectForKey:@"breif"];
                NSString * identifyS = [object objectForKey:@"identify"];
                NSString * priceS = [object objectForKey:@"price"];
                NSString * status = [object objectForKey:@"status"];

                
                [teacherNames addObject:teacherName];
                [otherStrs addObject:otherStr];
                [identifya addObject:identifyS];
                [priceSS addObject:priceS];
                [statusS addObject:status];

                
                NSString * imageUrlStr = [object objectForKey:@"imageUrl"];

//                NSMutableArray *birdtemp = [[NSMutableArray alloc] initWithObjects:object[@"imageUrl"], nil];
//                NSString * str = [NSString  stringWithFormat:@"%@",birdtemp];
                
                [strs addObject:imageUrlStr];
                
                
                
                
                ////NSLog(@"222%@",str);
                
            }
            [_tableView reloadData];
        }else{
            [MBProgressHUD hideHUDForView:_tableView animated:YES];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"对不起，你所在的地区没有麦苗公学。" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            
            [alert setTag:111];
            [alert show];
        }
        
        
    }];
    
    



}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    if (alertView.tag  == 111) {
        
        if (buttonIndex == 0) {
            
            
            [self dismissViewControllerAnimated:YES completion:nil];
            ////NSLog(@"1111111");
            
        }else if(buttonIndex == 1){
            
            
            ////NSLog(@"2222222");
            
            
        }
        
    }else {
        
    }
    
    
}


-(void)back{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}
-(void)baby{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
    
}

-(void)babys{
    
    
    
}


#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return teacherNames.count;
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    [MBProgressHUD hideHUDForView:_tableView animated:YES];
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
    }
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        
        UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"老师介绍线框@2x.png.png"]];
        imageView.frame = CGRectMake(5, 5, SCREEN_WIDTH-10, (SCREEN_HEIGHT-NavbarHight)/4-10);
        [cell addSubview:imageView];
        
    
        
        
        _imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH/3.5, (SCREEN_HEIGHT-NavbarHight)/4-20)];
        [_imageView sd_setImageWithURL:[NSURL  URLWithString:strs[indexPath.row]] placeholderImage:nil];
        
        
        [imageView addSubview:_imageView];
        
        
        _labDown = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, (SCREEN_HEIGHT-NavbarHight)/4/3-5, SCREEN_WIDTH-30-SCREEN_WIDTH/3.7-10, 80)];
        _labDown.text = otherStrs[indexPath.row];
        _labDown.font = [UIFont systemFontOfSize:12];
        _labDown.textAlignment = NSTextAlignmentLeft;
        _labDown.numberOfLines = 4;
        [imageView addSubview:_labDown];
        
        
        _labTyp = [[UILabel alloc]initWithFrame:CGRectMake(20+SCREEN_WIDTH/3.7, 10, 100, 20)];
        _labTyp.text = teacherNames[indexPath.row];
        _labTyp.font = [UIFont systemFontOfSize:12];
        _labTyp.textAlignment = NSTextAlignmentLeft;
        [imageView   addSubview:_labTyp];
        
        
    
    
    return cell;
    
    
}
#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
#pragma mark--- tableViewDelegat 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

//    //NSLog(@"%@",identifya[indexPath.row]);

    if ([statusS[indexPath.row] integerValue] == 0) {
        
        IndividualViewController *  IndividualVC = [[IndividualViewController alloc]init];
        IndividualVC.name = teacherNames[indexPath.row];
        IndividualVC.otherStr = otherStrs[indexPath.row];
        IndividualVC.identifya = identifya[indexPath.row];
        IndividualVC.pic = strs[indexPath.row];
        IndividualVC.manei = priceSS[indexPath.row];
        UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:IndividualVC];
        [self  presentViewController:nav animated:YES completion:nil];

        
    }else{
        
        
        
        alert(@"对不起，当前早教指导师档期已满");
 
        
    }
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
